package com.nf.health.app.widget.datePicker;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.nf.health.app.R;
import com.nf.health.app.widget.datePicker.adapter.NumericWheelAdapter;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MyDatePicker extends LinearLayout{
	private LayoutInflater inflate;
	private  Context context;
	private MyWheelView year;
	private MyWheelView month;
	private MyWheelView day;

	private int mYear=1996;
	private int mMonth=0;
	private int mDay=1;
	private  View view;
	public MyDatePicker(Context context, AttributeSet attrs) {
		super(context, attrs);
		initVew(context);
	}
	public MyDatePicker(Context context) {
		super(context);
		initVew(context);
	}
	private void initVew(Context context){
		this.context = context;
		inflate = LayoutInflater.from(context);
		View view = inflate.inflate(R.layout.wheel_date_picker,  null);//获取布局，开始初始化
		getDataPick();


	}
	private View getDataPick() {
		Calendar c = Calendar.getInstance();
		int norYear = c.get(Calendar.YEAR);
//		int curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
//		int curDate = c.get(Calendar.DATE);

		int curYear = mYear;
		int curMonth =mMonth+1;
		int curDate = mDay;
		year = (MyWheelView) view.findViewById(R.id.year);
		NumericWheelAdapter numericWheelAdapter1=new NumericWheelAdapter(context,1950, 2100);
		numericWheelAdapter1.setLabel("年");
		year.setViewAdapter(numericWheelAdapter1);
		year.setCyclic(true);//是否可循环滑动
		year.addScrollingListener(scrollListener);

		month = (MyWheelView) view.findViewById(R.id.month);
		NumericWheelAdapter numericWheelAdapter2=new NumericWheelAdapter(context,1, 12, "%02d");
		numericWheelAdapter2.setLabel("月");
		month.setViewAdapter(numericWheelAdapter2);
		month.setCyclic(true);
		month.addScrollingListener(scrollListener);

		day = (MyWheelView) view.findViewById(R.id.day);
		initDay(curYear,curMonth);
		day.setCyclic(true);

//		time= (WheelView) view.findViewById(R.id.time);
//		String[] times = {"上午","下午"} ;
//		ArrayWheelAdapter<String> arrayWheelAdapter=new ArrayWheelAdapter<String>(MainActivity.this,times );
//		time.setViewAdapter(arrayWheelAdapter);
//		time.setCyclic(false);
//		time.addScrollingListener(scrollListener);

		year.setVisibleItems(4);//设置显示行数
		month.setVisibleItems(4);
		day.setVisibleItems(4);
		//Toast.makeText(MainActivity.this, curYear+ "-------> : "+curMonth+ "  --  " +curDate , 0).show();
		year.setCurrentItem(curYear );
		month.setCurrentItem(curMonth );
		day.setCurrentItem(curDate );

		return view;
	}

	OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
		@Override
		public void onScrollingStarted(MyWheelView wheel) {

		}

		@Override
		public void onScrollingFinished(MyWheelView wheel) {
			int n_year = year.getCurrentItem() + 1950;//年
			int n_month = month.getCurrentItem() + 1;//月

			initDay(n_year,n_month);

			String birthday=new StringBuilder().append((year.getCurrentItem()+1950)).append("-").append((month.getCurrentItem() + 1) < 10 ? "0" + (month.getCurrentItem() + 1) : (month.getCurrentItem() + 1)).append("-").append(((day.getCurrentItem()+1) < 10) ? "0" + (day.getCurrentItem()+1) : (day.getCurrentItem()+1)).toString();
			//tv1.setText("年龄             "+calculateDatePoor(birthday)+"岁");
			//tv2.setText("星座             "+getAstro(month.getCurrentItem() + 1,day.getCurrentItem()+1));
		}
	};

	/**
	 *
	 * @param year
	 * @param month
	 * @return
	 */
	private int getDay(int year, int month) {
		int day = 30;
		boolean flag = false;
		switch (year % 4) {
			case 0:
				flag = true;
				break;
			default:
				flag = false;
				break;
		}
		switch (month) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				day = 31;
				break;
			case 2:
				day = flag ? 29 : 28;
				break;
			default:
				day = 30;
				break;
		}
		return day;
	}

	/**
	 */
	private void initDay(int arg1, int arg2) {
		NumericWheelAdapter numericWheelAdapter=new NumericWheelAdapter(context,1, getDay(arg1, arg2), "%02d");
		numericWheelAdapter.setLabel("日");
		day.setViewAdapter(numericWheelAdapter);
	}



	/**
	 * 根据日期计算年龄
	 * @param birthday
	 * @return
	 */
	public static final String calculateDatePoor(String birthday) {
		try {
			if (TextUtils.isEmpty(birthday))
				return "0";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date birthdayDate = sdf.parse(birthday);
			String currTimeStr = sdf.format(new Date());
			Date currDate = sdf.parse(currTimeStr);
			if (birthdayDate.getTime() > currDate.getTime()) {
				return "0";
			}
			long age = (currDate.getTime() - birthdayDate.getTime())
					/ (24 * 60 * 60 * 1000) + 1;
			String year = new DecimalFormat("0.00").format(age / 365f);
			if (TextUtils.isEmpty(year))
				return "0";
			return String.valueOf(new Double(year).intValue());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "0";
	}

	/**
	 * 根据月日计算星座
	 * @param month
	 * @param day
	 * @return
	 */
	public String getAstro(int month, int day) {
		String[] astro = new String[] { "摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座",
				"双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座" };
		int[] arr = new int[] { 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22 };// 两个星座分割日
		int index = month;
		// 所查询日期在分割日之前，索引-1，否则不变
		if (day < arr[month - 1]) {
			index = index - 1;
		}
		// 返回索引指向的星座string
		return astro[index];
	}

}
